package controller.admin;

import service.RecommendService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AdminGoodsRecommendServlet", value = "/admin/goods_recommend")
public class AdminGoodsRecommendServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RecommendService rs = new RecommendService();
        //标记操作 添加 "add" 删除 "remove"
        String method = request.getParameter("method");
        //商品id
        String id = request.getParameter("id");
        //所要操作商品的type类型
        String typeTarget = request.getParameter("typeTarget");
        //位于第几页
        String pageNumber = request.getParameter("pageNumber");
        //在那个选项卡
        String type = request.getParameter("type");

        //如果参数不完整
        if (!(method != null && !"".equals(method) && typeTarget != null && !"".equals(typeTarget) && id != null && !"".equals(id))) {
            String referer = request.getHeader("Referer");
            response.sendRedirect(referer);
            return;
        }
        //添加操作
        if ("add".equals(method)) {
            rs.addRecommend(Integer.parseInt(typeTarget), Integer.parseInt(id));
        } else if ("remove".equals(method)) { //删除操作
            rs.deleteRecommend(Integer.parseInt(typeTarget), Integer.parseInt(id));
        }
        String referer = request.getHeader("Referer");
        response.sendRedirect(referer);

    }
}
